package com.itboye.ihomebank.zjtlcb.lnd.livenessdetector.utils.security;

import org.apache.axiom.om.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 说明：${}
 * 作者：yangcheng
 * 邮箱：ycheng.dream@qq.com
 * Date: 2018/1/15.
 */

public class AESCipher {

    private static final String IV_STRING = "A-16-Byte-String";

    private static final String charset = "UTF-8";


    public static String encrypt(String content, String key) {
        try {
            byte[] contentBytes = content.getBytes(charset);
            byte[] keyBytes = key.getBytes(charset);
            byte[] encryptedBytes = aesEncryptBytes(contentBytes, keyBytes);
            return Base64.encode(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String decrypt(String content, String key) {
        try {
            byte[] encryptedBytes = Base64.decode(content);
            byte[] keyBytes = key.getBytes(charset);
            byte[] decryptedBytes = aesDecryptBytes(encryptedBytes, keyBytes);
            return new String(decryptedBytes, charset);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public static byte[] aesEncryptBytes(byte[] contentBytes, byte[] keyBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return cipherOperation(contentBytes, keyBytes, Cipher.ENCRYPT_MODE);
    }

    public static byte[] aesDecryptBytes(byte[] contentBytes, byte[] keyBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return cipherOperation(contentBytes, keyBytes, Cipher.DECRYPT_MODE);
    }


    private static byte[] cipherOperation(byte[] contentBytes, byte[] keyBytes, int mode) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        //cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(java.util.Base64.Decoder.decode(IV.
        SecretKeySpec keySpec=new SecretKeySpec(keyBytes,"AES");
        Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] initParam="abcdefghABCDEFGH".getBytes();
        IvParameterSpec ivParameterSpec=new IvParameterSpec(initParam);
//        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
//        byte[] initParam = IV_STRING.getBytes(charset);
//        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
//        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(mode, keySpec, ivParameterSpec);
        return cipher.doFinal(contentBytes);
    }

    public static void main(String[] args) {
        //CBC模式：AAtY2d3ts/GhbrsTqSGr62J21yxHV9fyZuovIPbgyjqkcrTu1z/k1hqk8zx8HPhb
        //IA02jTr32GLXAysvl6DPlsF77FZWXFjBz1uzIg4G72/OQFlcK+/K4kD1Tayi2JWU
        String content = "0f607264fc6318a92b9e13c65db7cd3c";
        String password = "mdzzmdzzmdzzmdzzmdzzmdzzmdzzmdzz";
        System.out.println("明文：" + content);
        System.out.println("key：" + password);
//
        try {
            String encrypt = encrypt(content, password);
            System.out.println("密文：" + encrypt);

            String decrypt = decrypt(encrypt, password);
            System.out.println("解密：" + decrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
